package com.zp.manager.message;

import com.zp.enums.ErrorCode;
import com.zp.exception.BusinessException;
import com.zp.utils.EmailUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

// 手动定义日志，避免 Lombok @Slf4j 注解处理缺失
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 邮箱服务
 */
@Component
public class EmailManager {
    private static final Logger log = LoggerFactory.getLogger(EmailManager.class);

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.nickname}")
    private String nickname;

    @Value("${spring.mail.username}")
    private String from;


    /**
     * 发送邮件验证码(发送验证码)
     *
     * @param to      邮件接收者
     * @param subject 邮件标题
     * @param code    验证码
     */
    @Async(value = "emailThreadPool")
    public void sendEmailCode(String to, String subject, String code) {
        log.info("发送邮件验证码[{}]到[{}]", to, code);
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        sendEmailAsCode(to, subject, map);
    }

    /**
     * 发送邮件(发送验证码)
     *
     * @param to         邮件接收者
     * @param subject    邮件标题
     * @param contentMap 邮件内容
     */
    public void sendEmailAsCode(String to, String subject, Map<String, Object> contentMap) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            // 组合发送邮箱的内容
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            // 设置邮件发送者
            helper.setFrom(nickname + " <" + from + ">");
            // 设置邮件接收者
            helper.setTo(to);
            // 设置邮件标题
            helper.setSubject(subject);
            // 设置邮件内容
            helper.setText(EmailUtils.emailContentTemplate("templates/EmailCodeTemplate.html", contentMap), true);
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("发送邮件失败", e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "发送邮件失败");
        }
    }

    /**
     * 发送注册成功邮件(发送初始密码)
     *
     * @param to      邮件接收者
     * @param subject 邮件标题
     */
    @Async(value = "emailThreadPool")
    public void sendEmailAsRegisterSuccess(String to, String subject) {
        try {
            // 创建MIME消息
            MimeMessage message = javaMailSender.createMimeMessage();
            // 组合邮箱发送的内容
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            // 设置邮件发送者
            messageHelper.setFrom(nickname + "<" + from + ">");
            // 设置邮件接收者
            messageHelper.setTo(to);
            // 设置邮件主题
            messageHelper.setSubject(subject);
            // 设置邮件内容
            messageHelper.setText(EmailUtils.emailContentTemplate("templates/EmailRegisterSuccessTemplate.html"), true);
            // 发送邮件
            javaMailSender.send(message);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "发送邮件失败");
        }
    }
}
